<template>
  <div>
    <button @click="addItem">添加项目</button>
    <button @click="removeItem">删除最后一项</button>

    <transition-group name="list" tag="ul" class="list-container">
      <li 
        v-for="(item, index) in items" 
        :key="item.id" 
        class="list-item"
      >
        {{ item.text }} (索引: {{ index }})
      </li>
    </transition-group>
  </div>
</template>

<script setup>
import { ref } from 'vue';

const items = ref([
  { id: 1, text: '项目1' },
  { id: 2, text: '项目2' },
  { id: 3, text: '项目3' }
]);

const addItem = () => {
  items.value.push({
    id: Date.now(),
    text: `项目${items.value.length + 1}`
  });
};

const removeItem = () => {
  if (items.value.length > 0) {
    items.value.pop();
  }
};
</script>

<style scoped>
.list-container {
  list-style: none;
  padding: 0;
  margin: 10px 0;
}

.list-item {
  padding: 8px;
  margin: 4px 0;
  background-color: #f0f0f0;
  border: 1px solid #ddd;
}

/* 列表项进入动画 */
.list-enter-active {
  transition: all 0.5s ease;
}
.list-enter-from {
  opacity: 0;
  transform: translateY(20px);
}

/* 列表项离开动画 */
.list-leave-active {
  transition: all 0.5s ease;
  position: absolute; /* 确保离开时不影响其他元素布局 */
}
.list-leave-from {
  opacity: 1;
  transform: translateY(0);
}
.list-leave-to {
  opacity: 0;
  transform: translateY(-20px);
}
</style>